package claude

// Request represents the request structure for Claude API on AWS Bedrock
type Request struct {
	AnthropicVersion string           `json:"anthropic_version"`
	Temperature      float64          `json:"temperature,omitempty"`
	MaxTokens        int              `json:"max_tokens,omitempty"`
	Tools            []ToolDefinition `json:"tools,omitempty"`
	System           string           `json:"system,omitempty"`
	Messages         []Message        `json:"messages"`
	TopP             float64          `json:"top_p,omitempty"`
	TopK             int              `json:"top_k,omitempty"`
	StopSequences    []string         `json:"stop_sequences,omitempty"`
}

// CacheControl represents Anthropic's cache_control field
type CacheControl struct {
	Type string `json:"type,omitempty"`
}

// ToolDefinition mirrors the JSON schema Bedrock expects.
type ToolDefinition struct {
	Name         string                 `json:"name"`                    // snake_case
	Description  string                 `json:"description,omitempty"`   // shown to the model
	InputSchema  map[string]interface{} `json:"input_schema"`            // JSON Schema Draft-07 (Bedrock expects snake_case)
	OutputSchema map[string]interface{} `json:"output_schema,omitempty"` // JSON Schema Draft-07 (Bedrock expects snake_case)
}

// Message represents a message in the Claude API request
type Message struct {
	Role    string         `json:"role"`
	Content []ContentBlock `json:"content"`
}

// ContentBlock represents a content block in a message
type ContentBlock struct {
	// Common fields ---------------------------------------------------------
	Type string `json:"type"` // "text", "image", "tool_use", "tool_result", ...

	// Text content ( when Type == "text" ) --------------------------------
	Text   string  `json:"text,omitempty"`
	Source *Source `json:"source,omitempty"`

	// Optional cache control for Bedrock/Anthropic
	CacheControl *CacheControl `json:"cache_control,omitempty"`

	// Tool invocation ( when Type == "tool_use" ) -------------------------
	ID    string                 `json:"id,omitempty"`    // tool_use id generated by model or client
	Name  string                 `json:"name,omitempty"`  // tool name to invoke
	Input map[string]interface{} `json:"input,omitempty"` // JSON arguments

	// Tool result ( when Type == "tool_result" ) -------------------------
	ToolUseID string      `json:"tool_use_id,omitempty"`
	Content   interface{} `json:"content,omitempty"`
}

// The older granular structs (Tool, ToolResultBlock, ToolResultContentBlock)
// were removed since Bedrock represents both tool_use and tool_result as flat
// objects within the Content array. Keeping a single ContentBlock with
// conditional fields aligns 1-to-1 with the raw JSON contract and avoids an
// extra nesting level during marshaling/unmarshaling.

// Source represents a source for image content
type Source struct {
	Type      string `json:"type"`
	MediaType string `json:"media_type,omitempty"`
	Data      string `json:"data,omitempty"`
}

// Response represents the response structure from Claude API on AWS Bedrock
type Response struct {
	ID                             string             `json:"id"`
	Type                           string             `json:"type"`
	Role                           string             `json:"role"`
	Content                        []ContentItem      `json:"content"`
	Model                          string             `json:"model"`
	StopReason                     string             `json:"stop_reason"`
	StopSequence                   string             `json:"stop_sequence"`
	Usage                          *Usage             `json:"usage"`
	AmazonBedrockInvocationMetrics *InvocationMetrics `json:"amazon-bedrock-invocationMetrics,omitempty"`
}

// ContentItem represents a content item in the response
type ContentItem struct {
	Type string `json:"type"`
	// Textual content when Type == "text"
	Text string `json:"text,omitempty"`

	// Tool invocation info when Type == "tool_use" ------------------------
	ID    string                 `json:"id,omitempty"`
	Name  string                 `json:"name,omitempty"`
	Input map[string]interface{} `json:"input,omitempty"`

	// Tool result when Type == "tool_result" ------------------------------
	ToolUseID string      `json:"tool_use_id,omitempty"`
	Content   interface{} `json:"content,omitempty"`
}

// Usage represents token usage information
type Usage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

// InvocationMetrics represents AWS Bedrock invocation metrics
type InvocationMetrics struct {
	InputTokenCount   int `json:"inputTokenCount"`
	OutputTokenCount  int `json:"outputTokenCount"`
	InvocationLatency int `json:"invocationLatency"`
	FirstByteLatency  int `json:"firstByteLatency"`
}
